#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "book.h"

#define ISBNBUFSIZE 15
#define TITLEBUFSIZE 2000
#define AUTHORBUFSIZE 2000
#define PRESSBUFSIZE 2000

static void map_book_info(book* new_book, char* isbn, char* title, char* author, char* press, int total, int rest)
{
    new_book->isbn = strdup(isbn);
    new_book->title = strdup(title);
    new_book->author = strdup(author);
    new_book->press = strdup(press);
    new_book->total = total;
    new_book->rest = rest;
}

book* create_books_list(char* books_db_path)
{
    FILE* books_db = initialize_file(books_db_path);
    if(books_db ==NULL)
        return NULL;
    book* first_book = NULL;
    book* current_book = NULL;
    char isbn_buf[ISBNBUFSIZE];
    char title_buf[TITLEBUFSIZE];
    char author_buf[AUTHORBUFSIZE];
    char press_buf[PRESSBUFSIZE];
    int total_buf = 0;
    int rest_buf = 0;
    while(feof(books_db) == 0)
    {
        if(fscanf(books_db, "%[0-9]$%[^$]$%[^$]$%[^$]$%d$%d\n", &isbn_buf, &title_buf, &author_buf, &press_buf, &total_buf, &rest_buf) == 6)
        {
            if(first_book == NULL)
            {
                first_book = (book*)malloc(sizeof(book));
                first_book->previous = NULL;
                first_book->next = NULL;
                map_book_info(first_book, isbn_buf, title_buf, author_buf, press_buf, total_buf, rest_buf);
                current_book = first_book;
            }
            else
            {
                book* temp_book = (book*)malloc(sizeof(book));
                temp_book->previous = current_book;
                temp_book->next = NULL;
                map_book_info(temp_book, isbn_buf, title_buf, author_buf, press_buf, total_buf, rest_buf);
                current_book->next = temp_book;
                current_book = temp_book;
            }
        }
        else
        {
            break;
        }
    }
    fclose(books_db);
    return first_book;
}

void free_book(book* target)
{
    free(target->isbn);
    free(target->title);
    free(target->author);
    free(target->press);
}

void show_book(book* books_list)
{
    if(books_list != NULL)
    {
        do
        {
            printf("--------------------\nISBN：%s\n标题：%s\n作者：%s\n出版社：%s\n可借总数：%d\n可借剩余：%d\n--------------------\n", books_list->isbn, books_list->title, books_list->author, books_list->press, books_list->total, books_list->rest);
        }
        while((books_list = books_list->next) != NULL);
    }
}

book* create_book()
{
    book* new_book = (book*)malloc(sizeof(book));
    char isbn_buf[ISBNBUFSIZE];
    char title_buf[TITLEBUFSIZE];
    char author_buf[AUTHORBUFSIZE];
    char press_buf[PRESSBUFSIZE];
    int total_buf = 0;
    printf("ISBN：");
    scanf("%s", &isbn_buf);
    fflush(stdin);
    if(strlen(isbn_buf) != 13)
    {
        printf("ISBN 应为 13 位\n");
        free(new_book);
        return NULL;
    }
    printf("标题：");
    scanf("%s", &title_buf);
    fflush(stdin);
    printf("作者：");
    scanf("%s", &author_buf);
    fflush(stdin);
    printf("出版社：");
    scanf("%s", &press_buf);
    fflush(stdin);
    printf("可借总数：");
    scanf("%d", &total_buf);
    fflush(stdin);
    while(total_buf < 0)
    {
        printf("数量应为正数\n");
        printf("可借总数：");
        scanf("%d", &total_buf);
        fflush(stdin);
    }
    new_book->next = NULL;
    new_book->previous =NULL;
    map_book_info(new_book, isbn_buf, title_buf, author_buf, press_buf, total_buf, total_buf);
    return new_book;
}

book* search_book(book* books_list, char* isbn)
{
    if(books_list != NULL)
    {
        book* result = NULL;
        do
        {
            if(strcmp(books_list->isbn, isbn) == 0)
            {
                result = books_list;
                break;
            }
        }
        while((books_list = books_list->next) != NULL);
        return result;
    }
    else
        return NULL;
}


int book_borrow_book(book* books_list, char* book_number)
{
    book* target_book = search_book(books_list, book_number);
    if(target_book != NULL)
    {
        if(target_book->rest > 0)
            target_book->rest -= 1;
        else
            return 0;
    }
    else
        return 0;
    return 1;
}

int book_return_book(book* books_list, char* book_number)
{
    book* target_book = search_book(books_list, book_number);
    if(target_book != NULL)
    {
        if(target_book->rest < target_book->total)
            target_book->rest += 1;
        else
            return 0;
    }
    else
        return 0;
    return 1;
}
